local AddOnName, Env = ...; local ADDON, DT = Env[1], Env[1].development
-- ~~| Libraries |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local DiesalGUI = LibStub('DiesalGUI-1.0')
-- ~~| Lua Upvalues |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local type, select, tostring, tonumber				= type, select, tostring, tonumber
local ipairs, pairs	 									= ipairs, pairs
local floor, modf 										= math.floor, math.modf
local sub, format, match, lower						= string.sub, string.format, string.match, string.lower
-- ~~| WoW Upvalues |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

-- ~~| ActionLogRow |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local Type 		= 'ActionLogRow'
local Version 	= 1
-- ~~| ActionLogRow StyleSheets |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local styleSheet = {
	['frame-background'] = {				
		type			= 'texture',
		layer			= 'BACKGROUND',		
		color			= '131313',		
		offset		= 0,		
	},
	['frame-topHighlight'] = {				
		type			= 'outline',
		layer			= 'BORDER',			
		color			= 'FFFFFF',		
		alpha			= .01,		
		offset		= {-18,0,0,nil},
		height		= 1,			
	},	
	['frame-inline'] = {				
		type			= 'outline',
		layer			= 'BORDER',			
		color			= 'FFFFFF',		
		alpha			= .03,		
		offset		= {-18,0,0,-1},	
	},		
	['frame-outline'] = {				
		type			= 'outline',
		layer			= 'BORDER',
		color			= '000000',
		offset		= {1,1,1,0},	
	},	
	['frame-iconDivide'] = {				
		type			= 'texture',
		layer			= 'BORDER',
		color			= '000000',
		offset		= {-17,nil,0,0},
		width			= 1,	
	},	
	
}
-- ~~| ActionLogRow Locals |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

local BLUE			= ADDON:GetTxtColor('00aaff') -- 100%
local BLUE1			= ADDON:GetTxtColor('00334d') -- 30%

local YELLOW		= ADDON:GetTxtColor('ffff00') -- 100%
local YELLOW1		= ADDON:GetTxtColor('4c4c00') -- 30%

local GREY			= ADDON:GetTxtColor('e6e6e6') -- 90%
local GREY1			= ADDON:GetTxtColor('4c4c4c') -- 30%

local function formatTime(num,col1,col2)
	if type(num) ~= 'number' then return '' end
	local seconds,ms = modf (num)
	
	local d = format("%02.f", floor(num/86400))
	local h = format("%02.f", floor(num/3600 - (d*24)))
	local m = format("%02.f", floor(num/60 - (h*60) -(d*1440)));
	local s = format("%02.f", floor(num - (m*60) - (h*3600) - (d*86400) ));
	ms = ms * 1000	
	
	return format("%s%s:%s%02.f:%03.f",col2,m,col1,s,ms)
end
-- ~~| ActionLogRow Methods |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local methods = {
	['OnAcquire'] = function(self)
		self:AddStyleSheet(styleSheet)
		-- self:AddStyleSheet(wireFrameSheet)
		self:Show()
	end,
	['OnRelease'] = function(self)

	end,
	['ApplySettings'] = function(self)
		local settings = self.settings
		local data 		= settings.data
		
		self:SetHeight(settings.rowHeight)		
		if settings.position == 1 then
			self:SetPoint('TOPLEFT') 
			self:SetPoint('RIGHT')			
		else			
			self:SetPoint('TOPLEFT',settings.rows[settings.position-1].frame,'BOTTOMLEFT',0,0)
			self:SetPoint('RIGHT')
		end
		
		-- local icon	
		-- if data.spellID then
		-- -	icon = select(3,GetSpellInfo(data.spellID == 0 and 4038 or data.spellID))
		-- else
		-- 	icon = select(3,GetSpellInfo(data.spell))
		-- end
		 
		-- self.rowIcon:SetTexture(icon)		
		-- self.rowCount:SetText(data.executeCount or '')	
		-- if data.mode then
		-- 	self.rowAction:SetText(modeColor[data.mode]..data.action)
		-- elseif data.action == 'SPELLCAST_SENT' then 
		-- 	self.rowAction:SetText(YELLOW..data.action)
		-- elseif data.action == 'SPELLCAST_SUCCEEDED' then	
		-- 	self.rowAction:SetText(BLUE..data.action)
		-- end
		
		-- 
		
		-- self.rowID:SetText(data.lineID or '')
		
		-- self.rowTime:SetText(formatGetTime(data.time))
	
		self.rowSpell:SetWidth(settings.spellColWidth or 1)				
	end,
	['ClearAction'] = function(self)	
		self.settings.data = nil
				
		self.rowIcon:Hide()	
		
		self.rowCount:SetText('')			
		self.rowSpell:SetText('')			
		self.rowAction:SetText('')
		self.rowID:SetText('')		
		self.rowTime:SetText('')
	end,
	['SetAction'] = function(self,data)	
		self.settings.data = data
		local icon	
		if data.spellID then
			icon = select(3,GetSpellInfo(data.spellID == 0 and 4038 or data.spellID))
		else
			icon = select(3,GetSpellInfo(data.spell))
		end
		self.rowIcon:Show()			
		self.rowIcon:SetTexture(icon)			
		self.rowCount:SetText(data.executeCount or '')			
		
		
		if data.mode then 
			self.rowSpell:SetText(GREY..data.spell)			                                            
			self.rowAction:SetText(GREY..data.action) 
			self.rowID:SetText(format(GREY..'%s', data.lineID or ''))	
			self.rowTime:SetText(formatTime(data.time,GREY,GREY1))  
		elseif data.action == 'SPELLCAST_SENT' then                   
			self.rowSpell:SetText(YELLOW..data.spell)			                                            
			self.rowAction:SetText(YELLOW..data.action) 
			self.rowID:SetText(format(YELLOW..'%s', data.lineID or ''))	
			self.rowTime:SetText(formatTime(data.time,YELLOW,YELLOW1))                 
		elseif data.action == 'SPELLCAST_SUCCEEDED' then	           
			self.rowSpell:SetText(BLUE..data.spell)			                                            
			self.rowAction:SetText(BLUE..data.action) 
			self.rowID:SetText(format(BLUE..'%s', data.lineID or ''))	
			self.rowTime:SetText(formatTime(data.time,BLUE,BLUE1))                  
		end
		
			
		
	end,
	['ShowTooltip'] = function(self)							
		if not ADDON.db.profile.actionLog.tooltips or not self.settings.data then return end		
		local data 	= self.settings.data
						
		GameTooltip:SetOwner(self.frame, "ANCHOR_CURSOR" , 0, 0)		
		GameTooltip:AddDoubleLine("Spell:",data.spell, 0, .66, 1, 1, 1,1)
		if data.spellID then GameTooltip:AddDoubleLine("SpellID:",data.spellID, 0, .66, 1, 1, 1,1) end
		GameTooltip:AddDoubleLine("Action:",data.action, 0, .66, 1, 1, 1,1)
		if data.rotation then GameTooltip:AddDoubleLine("Rotation:",format("%s|cff00aaff %s",data.rotation,data.author), 0, .66, 1, 1, 1,1)	end
		if data.mode then GameTooltip:AddDoubleLine("Mode:",data.mode, 0, .66, 1, 1, 1,1) end
		if data.mode then GameTooltip:AddDoubleLine("Execute Count:",data.executeCount, 0, .66, 1, 1, 1,1)	end	
		GameTooltip:Show()
		
	end,
			
}
-- ~~| ActionLogRow Constructor |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function Constructor()
	local self 		= DiesalGUI:CreateObjectBase(Type)
	local frame		= CreateFrame('Frame',nil,UIParent)		
	self.frame		= frame		
	-- ~~ Default Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	self.defaults = {

	}
	-- ~~ Registered Events ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	-- ~~ Construct ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	frame:SetScript("OnEnter", function(this)					
		self:ShowTooltip()
	end) 
	frame:SetScript("OnLeave", function(this)		
		GameTooltip:Hide()
	end) 	
	
	local rowIcon		= self:CreateRegion("Texture", 'rowIcon', frame)
	local rowCount		= self:CreateRegion("FontString", 'rowCount', frame, DiesalFontPixel2)
	local rowSpell 	= self:CreateRegion("FontString", 'rowSpell', frame)
	local rowAction 	= self:CreateRegion("FontString", 'rowAction', frame)
	local rowID 		= self:CreateRegion("FontString", 'rowID', frame)
	local rowTime 		= self:CreateRegion("FontString", 'rowTime', frame)

	rowCount:SetPoint('TOPLEFT',1,-4)
	rowCount:SetWidth(17)	
	rowCount:SetJustifyH("CENTER")	
	
	rowIcon:SetPoint('TOPLEFT')
	rowIcon:SetWidth(17)
	rowIcon:SetHeight(17)
	rowIcon:SetTexCoord(.08, .92, .08, .92)	
	
	rowSpell:SetPoint('TOPLEFT',rowIcon,'TOPRIGHT',6,-4)	
	rowSpell:SetJustifyH("LEFT")		

	rowAction:SetPoint('TOPLEFT',rowSpell,'TOPRIGHT',1,0)
	rowAction:SetPoint('TOPRIGHT',rowID,'TOPLEFT',-10,0)	
	rowAction:SetJustifyH("LEFT")		
	
	rowID:SetPoint('TOPRIGHT',rowTime,'TOPLEFT',-8,0)
	rowID:SetWidth(35)	
	rowID:SetJustifyH("CENTER")	
	
	rowTime:SetPoint('TOPRIGHT',0,-4)
	rowTime:SetWidth(57)	
	rowTime:SetJustifyH("LEFT")	
	
	-- ~~ Methods ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	for method, func in pairs(methods) do
		self[method] = func
	end
	-- ~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	DiesalGUI:RegisterObject(self)
	return self
end
DiesalGUI:RegisterObjectConstructor(Type,Constructor,Version)
